using System.Collections.Generic;
using System.Text;

namespace zevenseas.Community.Search
{
    public class WhereGroup
    {
        private List<WhereProperty> whereProperties;
        private GroupLogicalOperators? logOperator;

        public List<WhereProperty> WhereProperties
        {
            get { return whereProperties; }
            set { whereProperties = value; }
        }

        public GroupLogicalOperators? LogOperator
        {
            get { return logOperator; }
            set { logOperator = value; }
        }

        public WhereGroup()
        {
            whereProperties = new List<WhereProperty>();
        }

        private string GetPropertyString()
        {
            StringBuilder whereGroup = new StringBuilder();

            whereGroup.Append("(");
            
            foreach (WhereProperty property in whereProperties)
            {
                if (property.PropertyOperator == null)
                {
                    whereGroup.Insert(1, property.ToString());
                }
                else
                {
                    whereGroup.Append(property.ToString(true));
                }
            }
            whereGroup.Append(")");

            return whereGroup.ToString();
        }

        public string ToString(bool IncludeOperator)
        {
            string operatorString = "";

            switch (LogOperator)
            {
                case GroupLogicalOperators.AND:
                    operatorString = " AND ";
                    break;
                case GroupLogicalOperators.OR:
                    operatorString = " OR ";
                    break;
                default:
                    operatorString = "";
                    break;
            }

            return operatorString + ToString();
        }

        public override string ToString()
        {
            return GetPropertyString();
        }

    }
}